<template>
  <div>
    <!--顶部搜索栏-->
    <div>
    名称:
    <el-input v-model="fuzzy.name" style="width: 140px" size="small"></el-input>
    产地:
    <el-input
        v-model="fuzzy.locality"
        style="width: 140px"
        size="small"
    ></el-input>
    价格:
    <el-input
        v-model="fuzzy.minPrice"
        style="width: 140px"
        size="small"
    ></el-input
    >-
    <el-input
        v-model="fuzzy.maxPrice"
        style="width: 140px"
        size="small"
    ></el-input>
    创建日期:
    <el-date-picker
        v-model="fuzzy.startTime"
        type="date"
        placeholder="最早"
        value-format="yyyy-MM-dd"
    >
    </el-date-picker
    >-
    <el-date-picker
        v-model="fuzzy.endTime"
        type="date"
        placeholder="最晚"
        value-format="yyyy-MM-dd"
    >
    </el-date-picker>
    <br />
    <el-button
        type="primary"
        size="default"
        @click="handleSearch"
        style="margin-left: 10px"
    >搜索</el-button
    >
    <el-button
        type="primary"
        size="default"
        @click="handleAdd"
        style="margin-left: 10px"
    >新增</el-button
    ><br />
    </div>
    <!--表格-->
    <div>
    <el-table :data="commodities" style="width: 930px" stripe>
      <el-table-column label="序号" width="60" align="center">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="120" align="center">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120" align="center">
      </el-table-column>
      <el-table-column prop="locality" label="产地" width="120" align="center">
      </el-table-column>
      <el-table-column
          prop="createtime"
          label="创建日期"
          width="240"
          align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="270" align="center">
        <template #default="scope">
          <el-button
              type="primary"
              size="default"
              style="margin-right: 5px"
              @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
              title="确定要删除吗？"
              @confirm="handleDelete(scope.row.carid)"
          >
            <template v-slot:reference>
              <el-button
                  type="danger"
                  size="default"
                  style="margin-right: 5px"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
          <el-button
              type="primary"
              size="default"
              style="margin-right: 5px"
              @click="showAccessory(scope.row.carid)"
          >
            附属品
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <!--分页-->
    <div>
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
    >
    </el-pagination>
    </div>
    <!--编辑对话框-->
    <div>
    <el-dialog title="编辑" v-model="editDialogVisible" @close="cancel">
      <el-form :model="editCommodity" label-width="70px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="editCommodity.name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="editCommodity.price"></el-input>
        </el-form-item>
        <el-form-item label="产地" prop="locality">
          <el-input v-model="editCommodity.locality"></el-input>
        </el-form-item>
        <el-form-item label="创建日期" prop="createtime">
          <el-date-picker
              v-model="editCommodity.createtime"
              type="date"
              placeholder="选择创建日期"
              value-format="yyyy-MM-dd"
              readonly
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="commitEdit">确 定</el-button>
      </div>
    </el-dialog>
    </div>
    <!--新增对话框-->
    <div>
    <el-dialog title="新增" v-model="addDialogVisible" @close="cancel">
      <el-form :model="addCommodity" label-width="70px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="addCommodity.name"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="addCommodity.price"></el-input>
        </el-form-item>
        <el-form-item label="产地" prop="locality">
          <el-input v-model="addCommodity.locality"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="commitAdd">确 定</el-button>
      </div>
    </el-dialog>
    </div>
    <!--附属品管理对话框-->
    <Accessory ref="accessoryRef"></Accessory>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref,onMounted } from 'vue';
import {fuzzySearch, findPages, deleteOne, update, insert} from "../services/Commodity.ts";
import Accessory from '../components/Accessory.vue';
import {ElMessage} from "element-plus"; // 假设附属品组件在同一个目录下

interface Commodity {
  name: string;
  price: number;
  locality: string;
  createtime: string;
  carid: string;
}

interface FuzzySearch {
  pageSize?: number;
  pageNum?: number;
  name?: string;
  locality?: string;
  minPrice?: number;
  maxPrice?: number;
  startTime?: string;
  endTime?: string;
}

export default defineComponent({
  components: {
    Accessory,
  },
  setup() {
    const fuzzy = reactive<FuzzySearch>({});

    const commodities = ref<Commodity[]>([]);
    const pageNum = ref(1);
    const pageSize = ref(10);
    const total = ref(0);
    const isFuzzy=ref(false);
    const accessoryRef = ref(null);

    const editDialogVisible = ref(false);
    const addDialogVisible = ref(false);
    const editCommodity = reactive<Commodity>({
      carid: "",
      name: "",
      price: 0,
      locality: "",
      createtime: "",
    });
    const addCommodity = reactive<Commodity>({
      name: '',
      price: 0,
      locality: '',
      createtime: '',
      carid: '',
    });
    onMounted(()=>{
      findPages(pageNum.value,pageSize.value).then((res) => {
        commodities.value = res.data.data.rows;
        total.value = res.data.data.total;
      });
      handleSearch();
    })

    const handleSearch = () => {
      fuzzy.pageSize = pageSize.value;
      fuzzy.pageNum = pageNum.value;
      fuzzySearch(fuzzy).then((res) => {
        commodities.value = res.data.data.rows;
        total.value = res.data.data.total;
        isFuzzy.value=true;
      });
    };
    const refresh=()=>{
      if(isFuzzy.value){
        handleSearch();
      }else {
        findPages(pageNum.value,pageSize.value).then((res) => {
          commodities.value = res.data.data.rows;
          total.value = res.data.data.total;
        });
      }
    }

    const handleCurrentChange = (newPageNum:number) => {
      pageNum.value = newPageNum;
      refresh();
    }

    const handleAdd = () => {
      addDialogVisible.value = true;
    };

    const handleEdit = (row: Commodity) => {
      editCommodity.name = row.name;
      editCommodity.price = row.price;
      editCommodity.locality = row.locality;
      editCommodity.createtime = row.createtime;
      editCommodity.carid = row.carid;
      editDialogVisible.value = true;
    };

    const handleDelete = (carid: string) => {
      deleteOne(carid).then(() => {
        ElMessage.success('删除成功');
        refresh();
      });
    };

    const cancel = () => {
      editDialogVisible.value = false;
      addDialogVisible.value = false;
    }

    const commitEdit = () => {
      editDialogVisible.value = false;
      update(editCommodity).then(() => {
        ElMessage.success('修改成功');
        refresh();
      });
    }
    const commitAdd = () => {
      editDialogVisible.value = false;
      insert(addCommodity).then(() => {
        ElMessage.success('新增成功');
        refresh();
        addCommodity.name = '';
        addCommodity.price = 0;
        addCommodity.locality = '';
        addCommodity.createtime = '';
        addCommodity.carid = '';
      });
    }

    const showAccessory = (carid: string) => {
      (accessoryRef.value as any).show(carid);
    };



    return {
      fuzzy,
      commodities,
      pageNum,
      pageSize,
      total,
      editDialogVisible,
      addDialogVisible,
      editCommodity,
      addCommodity,
      handleSearch,
      handleAdd,
      handleEdit,
      handleDelete,
      showAccessory,
      handleCurrentChange,
      cancel,
      commitEdit,
      commitAdd,
      accessoryRef
    };
  },
});
</script>
